//
//  TXFBasicView.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/6/25.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit
import SnapKit
class TXFBasicView: UIView {
   var closure:(()->())?
    override init(frame: CGRect) {
    
        super.init(frame: frame)
        setUpUI()
    
    }

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
     }
    
//MARK: -懒加载
   //图片
private  lazy var feedImageView :UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮照图片
private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //房子
private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    //lab文字
    
private lazy var messagelabel : UILabel = {
    let lab = UILabel()
    lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
    lab.textColor  = UIColor.orangeColor()
    lab.numberOfLines = 0
    lab.textAlignment = .Center
    lab.font = UIFont.systemFontOfSize(14)
    return lab
    }()
    
   //登录按钮
    private lazy var loginButton : UIButton = {
    
    let button = UIButton()
    button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
    button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
   
    button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
    button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
    
    return button
    }()
    
    //注册按钮
    private lazy var registerButton : UIButton = {
        
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
  private  func setUpUI(){
  backgroundColor = UIColor(red: 237/256, green: 237/256, blue: 237/256, alpha: 1.0)
   //创建控件:
        
    addSubview(feedImageView)
    addSubview(maskImageView)
    addSubview(iconImageView)
    addSubview(messagelabel)
    addSubview(loginButton)
    addSubview(registerButton)
       feedImageView.snp_makeConstraints { (make) -> Void in
        
        make.center.equalTo(self)
      
        }
 
        maskImageView.snp_makeConstraints { (make) -> Void in
            
        make.center.equalTo(self)
        }
      iconImageView.snp_makeConstraints { (make) -> Void in
        
        make.center.equalTo(self)
        
      }
        messagelabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.centerX.equalTo(maskImageView)
            
        
    }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.left.equalTo(messagelabel)
            
            make.top.equalTo(messagelabel.snp_bottom).offset(16)
            
    }
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.trailing.equalTo(messagelabel)
            
            make.top.equalTo(messagelabel.snp_bottom).offset(16)
            
    }
    
    }
  
    func setupAnim(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.duration = 10
        
        anim.removedOnCompletion = false
        
        anim.toValue = M_PI * 2
        
        anim.repeatCount = MAXFLOAT
        
        feedImageView.layer.addAnimation(anim, forKey: nil)
        
        
        
        
    }

    
    //由于每个控制器的登录界面都不一样 ,所以要判断
    
    func setupBasicViewInfo(imgName : String?,title : String?){
    
    //如果img,title存在说明是在首页 ,不存在说明在别的控制器
        
        if let img = imgName , tit = title {
        
        iconImageView.image = UIImage(named: img)
        
        messagelabel.text = tit
        
        feedImageView.hidden = true
        }else {
        
        
        //主页  不用设置
            setupAnim()
        
        }
    
    
    }
    
    
    //点击方法
    
    @objc private func loginClick(){
    
    closure?()
 
    }

    
}
